/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/09/19
*/
#include "Text.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

Text* Text::GetInterface()
{
    Text* text = new Text;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[Text] new text : %d", text);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("Text", text);
#endif
    return text;
}

void Text::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[Text] delete text : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

Text* Text::ToText(Control* ctrl)
{
    return dynamic_cast<Text*>(ctrl);
}

Text::Text()
{}

Text::~Text()
{}

Size Text::GetFixedSize() const
{
    if (!m_bAutoHeight && !m_bAutoWidth) return Size(m_rcFixed.w, m_rcFixed.h);

    int w = m_rcFixed.w;
    int h = m_rcFixed.h;

    IFont* font = this->GetFont();
    if (font){
        const char* text = m_text.GetString();
        if (m_bTranslateText) text = IEnginePtr->GetText(m_text.GetString(), 0);

        //do something.
    }
    
    return Size(w, h);
}

void Text::RenderText(IRender* render, const Rect* rc)
{
    if (m_text.IsEmpty()) return;

    //get text
    const char* text = this->GetTranslateText();
    if (!text) return;

    //get font
    IFont* font = this->GetFont();
    if (!font) return;

    //render
    //Size sz = font->GetSizeUTF8(text);    
    //Rect rc = this->GetTextAlignRect(&sz);
    render->DrawWrappedText(text, &m_rc, font, &m_textColor);

}

EDUI_NAMESPACE_END
